<?php
require_once('../../../config.php');
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/local/blackboxsurvey/lib.php');
require_once($CFG->dirroot . "/local/blackboxsurvey/forms.php");
define('AJAX_SCRIPT', true);

//yeu cau login,sau khi login tu dong chuyen sang trang chu
global $USER;
if ($USER->id == null){
    $SESSION->homepage = true;
    require_login($SITE);
}
if($SESSION->homepage){
    $SESSION->homepage = null;
    redirect( $CFG->wwwroot);
}

//class
require_once($CFG->dirroot . "/local/blackboxsurvey/class/survey.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/questionaire.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/questions.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/question_choice.php");

//require_capability('local/blackboxsurvey:adminmanagement', context_system::instance());
//path
$path = $CFG->wwwroot.'/local/blackboxsurvey/lecturer/manage_questionaire.php';
$path_image = $CFG->wwwroot .'/local/blackboxsurvey/image/';
$array['path_image'] = $path_image;
try{
$id = required_param('idbbsurvey', PARAM_TEXT);
$id_subject = required_param('idsubject', PARAM_TEXT);

//tao id cho questionaire
global $USER;
$c_qn = new b_questionaire();
$qn_id = $c_qn->create_questionaire_id($id,$USER->id,$id_subject);
$array['questionaire_id'] = $qn_id;
//xoa cac du lieu luu tam truoc do
$c_qn->delete_temp_questionaire_data($qn_id);
//cho nay goi cai dong ham lay chi tiet phieu khao sat
$c_survey = new b_survey();
$b_ques = new b_questions();
$b_ques_choice = new b_question_choice();
$survey = $c_qn->get_questionaire_info($id,$id_subject);//lay thong tin co ban cua phieu khao sat

//gan nhung du lieu vao $arry de hien tren form
$array['program'] = $survey->program_name;
$array['subject'] = $survey->subject_name_vn;
$array['start'] = $survey->date_start;
$array['end'] = $survey->date_end;
$array['id_program'] = $survey->p_id;

//lay cau hoi cua phieu khao sat
$questions = $c_survey->get_survey_question_bySurveyID($id);
$array['ques_num'] = count($questions);
$count = 1;

//lay cau hoi da add vao survey
foreach($questions as $q) {
    $q_content = preg_replace(array('#<p>#', '#</p>#'), '', $q->content, 1);
    $array['ques_id'.$count] = $q->id;
    $array['ques_content'.$count] = $count.'- '.$q_content;
    $array['type'.$count] = $q->id_type;
    $ans = $c_qn->view_question_answer($qn_id, $q->id, $q->id_type);
    //echo "<script  type='text/javascript'>alert('".$qn_id.','.$q->id.','.$q->id_type.','."');</script>";
    if($q->id_type == 1 || $q->id_type == 4)
    {
        $array['ans'.$count] = $ans->answer_content;
        
    }
    elseif($q->id_type == 2 || $q->id_type==  3)
    {
        //goi ham lay tat ca choice cua cau hoi
        $ques_choice = $b_ques_choice->get_choice_by_question($q->ques_id);
        //dem so luong option
        $array['num_choice'.$count] = count($ques_choice);
        
        //chay foreach de gan choice vao array
        $n = 1;
        
        foreach($ques_choice as $c){
            $array['choice_id'.$count.$n] = $c->id;
            $array['choice_content'.$count.$n] = $c->content;
            
            //chay for de kiem tra choice co dc chon chua
            foreach($ans as $a){
                if($c->id == $a->id_choice)
                    $array['choice_ck'.$count.$n] = 'checked';
                else
                    $array['choice_ck'.$count.$n] = '';
            }
            $n++;
        }
    }
    
    $count++;  
}
//Lay guide in
$array['guide_in'] = $survey->guide_input;
//Lay guide out
$array['guide_out'] = $survey->guide_output;
//Lay print
$array['print_line'] = $survey->print_line;
//Lay in, out, provide subject in, provide subject out
$array['view_in'] = $c_qn->view_questionaire_inlo($qn_id);
$array['view_out'] = $c_qn->view_questionaire_outlo($qn_id);
$array['view_psi'] = $c_qn->view_insub_list_questionaire($qn_id);
$array['view_pso'] = $c_qn->view_outsub_list_questionaire($qn_id);
}catch(Exception $ex){}


//Them thanh menu ngang ung voi trang trong duong link
navigation_node::override_active_url(new moodle_url($path, array('param'=>'value')));

/* Chuan bi trang web can hien thi */	


$url = new moodle_url($path);
$PAGE->set_url($url);
$PAGE->set_title('View Questionnaire');
$PAGE->set_heading('View Questionnaire ');
$PAGE->set_pagelayout('standard');

echo $OUTPUT->header();

$mform = new form_view_questionaire(null, $array);
/* Viet doan code cua ban o day */
if ($fromform = $mform->get_data()) 
{	
    
    
    //echo "<script  type='text/javascript'>alert('".$fromform->txt_action."');</script>";
}


$mform->display();
/*------Ket thuc doan code cua ban-------*/

echo $OUTPUT->footer();